jetcrab\bytecode\statements/
class.rs1use crate::ast::Node;
2use crate::vm::instructions::Instruction;
3
4pub trait ClassGenerator {
5 fn generate_class_declaration(&mut self, node: &Node);
6 fn generate_class_expression(&mut self, node: &Node);
7}
8
9pub trait ClassCore {
10 fn instructions(&mut self) -> &mut Vec<Instruction>;
11 fn visit_node(&mut self, node: &Node);
12}
13
14impl<T> ClassGenerator for T
15where
16 T: ClassCore,
17{
18 fn generate_class_declaration(&mut self, node: &Node) {
19 if let Node::ClassDeclaration(decl) = node {
20 if let Some(id) = &decl.id {
21 self.visit_node(id);
22 }
23 if let Some(super_class) = &decl.super_class {
24 self.visit_node(super_class);
25 }
26 self.visit_node(&decl.body);
27 self.instructions().push(Instruction::NewClass);
28 }
29 }
30
31 fn generate_class_expression(&mut self, node: &Node) {
32 if let Node::ClassExpression(expr) = node {
33 if let Some(id) = &expr.id {
34 self.visit_node(id);
35 }
36 if let Some(super_class) = &expr.super_class {
37 self.visit_node(super_class);
38 }
39 self.visit_node(&expr.body);
40 self.instructions().push(Instruction::NewClass);
41 }
42 }
43}